package com.easyidea.its.domain.repository.surveillance.impl;

import java.util.Collection;

import org.hibernate.criterion.Restrictions;

import com.easyidea.its.domain.model.surveillance.Surveillance;
import com.easyidea.its.domain.repository.base.impl.BaseBeanRepositoryImpl;
import com.easyidea.its.domain.repository.surveillance.SurveillanceRepository;
import com.easyidea.its.util.ParamsUtilities;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.action.surveillance.dto.SurveillanceDto;

/**
 * 
 * @author kangning.hu
 * 
 */
public class SurveillanceRepositoryImpl extends BaseBeanRepositoryImpl<Surveillance> implements SurveillanceRepository {

	public int getSurveillanceCountByDto(SurveillanceDto searchDto) {
		String hql = "select count(distinct Surveillance.id) " + this.getAssemblyHql(searchDto);
		return this.getBeanByHQL(Long.class, hql).intValue();
	}

	public Collection<Surveillance> getSurveillanceByDto(SurveillanceDto searchDto, int start, int pageSize) {
		String hql = "select Surveillance " + this.getAssemblyHql(searchDto);
		Collection<Surveillance> surveillances = this.listBeansByHQL(Surveillance.class, hql, start, pageSize);
		return surveillances;
	}

	private String getAssemblyHql(SurveillanceDto searchDto) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(" from " + Surveillance.class.getName() + " as Surveillance ");

		StringBuffer whereBuffer = new StringBuffer();

		if (!StringUtil.isNullOrBlank(searchDto.getSurveillanceType())) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.surveillanceType='" + searchDto.getSurveillanceType() + "' ");
			else
				whereBuffer.append(" where Surveillance.surveillanceType='" + searchDto.getSurveillanceType() + "' ");
		}
		if (!StringUtil.isNullOrBlank(searchDto.getPlateNumberSufix())) {
			String plateNumber = searchDto.getPlateNumberSufix();
			final StringBuffer finalStr = new StringBuffer();
			for (int i = 0; i < plateNumber.length(); i++) {
				finalStr.append(ParamsUtilities.convertChar(plateNumber.charAt(i)));
			}
			
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.plateNumber like '" + finalStr.toString() + "' ");
			else
				whereBuffer.append(" where Surveillance.plateNumber like '" + finalStr.toString() + "' ");
		}		
		if (!StringUtil.isNullOrBlank(searchDto.getStartDate())) {
			String startDate = searchDto.getStartDate();
			startDate = startDate+" "+searchDto.getStartHour()+":"+searchDto.getStartMinute()+":00";
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.startingDate>='" + startDate + "' ");
			else
				whereBuffer.append(" where Surveillance.startingDate>='" + startDate + "' ");
		}
		if (!StringUtil.isNullOrBlank(searchDto.getEndDate())) {
			String endDate = searchDto.getEndDate();
			endDate = endDate+" "+searchDto.getEndHour()+":"+searchDto.getEndMinute()+":00";
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.expiringDate<='" + endDate + "' ");
			else
				whereBuffer.append(" where Surveillance.expiringDate<='" + endDate + "' ");
		}		
		if (!StringUtil.isNullOrBlank(searchDto.getPlateType())) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.plateType='" + searchDto.getPlateType() + "' ");
			else
				whereBuffer.append(" where Surveillance.plateType='" + searchDto.getPlateType() + "' ");
		}
		if (searchDto.isActive() != null) {
			if (whereBuffer.length() > 0)
				whereBuffer.append(" and Surveillance.active = " + searchDto.isActive() );
			else
				whereBuffer.append(" where Surveillance.active= " + searchDto.isActive());
		}
		buffer.append(whereBuffer);
		return buffer.toString();
	}
}
